﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Googler.BLL;
using Googler.DAL;
using HodhodNews.Aggregation;
using HodhodNews.Controllers;
using HodhodNews.Web;

namespace Googler.Web
{
    public partial class Saliss : System.Web.UI.MasterPage, IHodhodMaster
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblLastAggregationValue.Text = Sessions.AggregationLastUpdate.ToArabicHumman();
            lblAggregationPeriod.Text = string.Format(lblAggregationPeriod.Text, Sessions.AggregationTimerPeriod);
            string appStartedFrom = ".... الموقع يعمل بلا انقطاع منذ <b>{0}</b>";
            lblAppStartedFrom.Text = string.Format(appStartedFrom, Sessions.AppStartedAt.ToArabicHumman());
            RefreshKeywords();
        }

        public void RefreshKeywords()
        {
            rptKeywords.DataSource = new UserController().GetKeywords();
            rptKeywords.DataBind();
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            DoNewsAggregation();
            Response.Redirect("~");
        }

        public void DoNewsAggregation()
        {
            try
            {
                Application["AggregationInProgress"] = "Yes";
                var aggregator = NewsAggregationProvider.Instance;
                var list = aggregator.Aggregate(new AggregationOptions()
                {
                    Keywords = new UserController().GetKeywords()
                });
            }
            finally
            {
                Application["AggregationInProgress"] = null;
            }
        }


    }
}